package com.qk.controller;

import com.qk.commen.PageResult;
import com.qk.commen.Result;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程管理
 */
@Slf4j
@RequestMapping("/courses")
@RestController

public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * select course list
     */
    @GetMapping
    public Result list(CourseDto courseDto) {
        log.info("课程查询列表 ,{}", courseDto);
        PageResult<Course> pageResult = courseService.list(courseDto);
        return Result.success(pageResult);
    }

    /**
     * delete course
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除课程: {}", id);
        courseService.delete(id);
        return Result.success();
    }

    /**
     * add course
     */
    @PostMapping
    public Result add(@RequestBody Course course) {
        log.info("添加课程: {}", course);
        courseService.add(course);
        return Result.success();
    }

    /**
     * select course by id
     */
    @GetMapping("/{id}")
    public Result getCourseById(@PathVariable Integer id) {
        log.info("查询课程: {}", id);
        Course course = courseService.getCourseById(id);
        return Result.success(course);
    }

    /**
     * update course
     */

    @PutMapping
    public Result update(@RequestBody Course course) {
        log.info("更新课程: {}", course);
        courseService.update(course);
        return Result.success();
    }

    /**
     * select all course
     */
    @GetMapping("/list")
    public Result listAll() {
//        log.info("查询所有课程: {}", courseService.listAll());
        List<Course> courseList = courseService.listAll();
        return Result.success(courseList);
    }

    /**
     * select course by subject
     */
    @GetMapping("subject/{subject}")
    public Result listBySubject(@PathVariable Integer subject) {
        log.info("查询课程: {}", courseService.listBySubject(subject));
        List<Course> courseList = courseService.listBySubject(subject);
        return Result.success(courseList);
    }



}
